home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ImageMagick / xtp / network.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.5 KB  |  133 lines

  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %                                                                             %
  4. %                                                                             %
  5. %                                                                             %
  6. %             N   N  EEEEE  TTTTT  W   W   OOO   RRRR   K   K                 %
  7. %             NN  N  E        T    W   W  O   O  R   R  K  K                  %
  8. %             N N N  EEE      T    W W W  O   O  RRRR   KKK                   %
  9. %             N  NN  E        T    WW WW  O   O  R R    K  K                  %
  10. %             N   N  EEEEE    T    W   W   OOO   R  R   K   K                 %
  11. %                                                                             %
  12. %                                                                             %
  13. %                          Network Routines.                                  %
  14. %                                                                             %
  15. %                                                                             %
  16. %                           Software Design                                   %
  17. %                             John Cristy                                     %
  18. %                             October 1992                                    %
  19. %                                                                             %
  20. %                                                                             %
  21. %  Copyright 1993 E. I. Dupont de Nemours & Company                           %
  22. %                                                                             %
  23. %  Permission to use, copy, modify, distribute, and sell this software and    %
  24. %  its documentation for any purpose is hereby granted without fee,           %
  25. %  provided that the above Copyright notice appear in all copies and that     %
  26. %  both that Copyright notice and this permission notice appear in            %
  27. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  28. %  & Company not be used in advertising or publicity pertaining to            %
  29. %  distribution of the software without specific, written prior               %
  30. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  31. %  about the suitability of this software for any purpose.  It is provided    %
  32. %  "as is" without express or implied warranty.                               %
  33. %                                                                             %
  34. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  35. %  to this software, including all implied warranties of merchantability      %
  36. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  37. %  liable for any special, indirect or consequential damages or any           %
  38. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  39. %  in an action of contract, negligence or other tortious action, arising     %
  40. %  out of or in connection with the use or performance of this software.      %
  41. %                                                                             %
  42. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  43. %
  44. %
  45. */
  46.  
  47. #include "xtp.h"
  48. #include "regular.h"
  49. #include <unistd.h>
  50. #include <sys/socket.h>
  51. #include <netinet/in.h>
  52. #include <netdb.h>
  53. #include <arpa/inet.h>
  54.  
  55.  
  56. /*
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. %                                                                             %
  59. %                                                                             %
  60. %                                                                             %
  61. %   G e t H o s t I n f o                                                     %
  62. %                                                                             %
  63. %                                                                             %
  64. %                                                                             %
  65. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  66. %
  67. %  Function GetHostInfo accepts a host name or address, verifies it is valid,
  68. %  and returns both the host name and address from the network host entry.
  69. %
  70. %  The format of the GetHostInfo routine is:
  71. %
  72. %    info=GetHostInfo(name)
  73. %
  74. %  A description of each parameter follows:
  75. %
  76. %    o info:  Function GetHostInfo returns a pointer to the host name and
  77. %      IP address.  A null pointer is returned if there the host cannot be
  78. %      located.
  79. %
  80. %    o name:  Specifies a pointer to a character array that contains either
  81. %      a name of a host or an IP address.
  82. %
  83. %
  84. */
  85. char *GetHostInfo(name)
  86. char
  87.   *name;
  88. {
  89.   char
  90.     *p;
  91.  
  92.   static char
  93.     info[2048];
  94.  
  95.   struct in_addr
  96.     in;
  97.  
  98.   struct hostent
  99.     *hp;
  100.  
  101.   /*
  102.     Get host name and address.
  103.   */
  104.   if (isascii(*name) && isdigit(*name))
  105.     in.s_addr=inet_addr(name);
  106.   else
  107.     {
  108.       in.s_addr=(unsigned long) -1;
  109.       hp=gethostbyname(name);
  110.       if (hp != (struct hostent *) NULL)
  111.         in.s_addr=(*(int *) hp->h_addr);
  112.     }
  113.   hp=gethostbyaddr((char *) &in,sizeof(struct in_addr),AF_INET);
  114.   if (hp == (struct hostent *) NULL)
  115.     {
  116.       hp=gethostbyname(name);
  117.       if (hp == (struct hostent *) NULL)
  118.         return((char *) NULL);
  119.     }
  120.   /*
  121.     Convert hostname to lower-case characters.
  122.   */
  123.   p=hp->h_name;
  124.   while (*p)
  125.   {
  126.     if (isupper(*p))
  127.       *p=tolower(*p);
  128.     p++;
  129.   }
  130.   (void) sprintf(info,"%s [%s]: ",hp->h_name,inet_ntoa(in));
  131.   return(info);
  132. }
  133.